package br.com.comanda.webservice.resources;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import br.com.comanda.webservice.business.ItemBusiness;
import br.com.comanda.webservice.model.Item;

@Path("/items")
public class ItemResources {

	private final ItemBusiness itemBusiness = new ItemBusiness();

	@POST
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.TEXT_HTML })
	public String addItem(Item item) {
		this.itemBusiness.save(item);
		return "ok";
	}

	@Path("{id}")
	@DELETE
	@Consumes({ MediaType.TEXT_PLAIN })
	@Produces({ MediaType.TEXT_HTML })
	public String removeItem(@PathParam("id") int id) {
		this.itemBusiness.remove(id);
		return "ok";
	}

	@Path("{accountId}")
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Item> getItemsByAccount(@PathParam("accountId") int accountId) {
		return this.itemBusiness.findItensByAccount(accountId);
	}

	@PUT
	@Consumes({ MediaType.APPLICATION_JSON })
	@Produces({ MediaType.TEXT_HTML })
	public String updateItem(Item item) {
		System.out.println(item.toString());
		this.itemBusiness.updateItem(item);
		return "ok";
	}

}
